package com.xdf.xzymanagementsystem.controller.system;

import com.xdf.xzymanagementsystem.base.BaseController;
import com.xdf.xzymanagementsystem.enums.*;
import com.xdf.xzymanagementsystem.service.SysConfigVarcharService;
import com.xdf.xzymanagementsystem.service.SysUserService;
import com.xdf.xzymanagementsystem.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Optional;

/**
 * @program: xzy-management-system
 * @description: 视图控制器
 * @author: 张柯
 * @create: 2020-12-31 08:43
 **/
@Api(tags = "后端路由")
@Controller
public class ModelRouterController extends BaseController {
    @Value("${server.port}")
    private String port;

    @Value("${reportJavascriptURL}")
    private String reportURL;

    @Value("${app.version}")
    private String version;

    private static final String MENUID = "MENU_ID";
    private static final String WELCOME = "WELCOME";
    private static final String REPORT_SERVER_URL = "REPORT_SERVER_URL";
    private static final String VERSION = "VERSION";

    private SysConfigVarcharService configVarcharService;
    private SysUserService sysUserService;

    @Autowired
    public void setConfigVarcharService(SysConfigVarcharService configVarcharService) {
        this.configVarcharService = configVarcharService;
    }

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/login")
    @ApiOperation(value = "登录页面", httpMethod = "GET", notes = "页面接口")
    public String login(Model model) throws Exception {
        if (isLogin()) {
            return "redirect:/home";
        }
        String name = Optional.ofNullable(configVarcharService
                        .selectByPrimaryKey(ConfigVarEnum.CLIENT_TITLE.getField()))
                .map(x -> x.getConfigValue()).orElse("");
        model.addAttribute(ConfigVarEnum.CLIENT_TITLE.name(), name);
        return "pages/login/login";
    }

    /**
     * 404错误页面
     *
     * @return
     */
    @RequestMapping("/404")
    @ApiOperation(value = "404错误页面", httpMethod = "GET", notes = "页面接口")
    public String error404() {
        return "error/404";
    }

    /**
     * 无权访问
     *
     * @return
     */
    @RequestMapping("/unauthorized")
    @ApiOperation(value = "无权限", httpMethod = "GET", notes = "页面接口")
    public String unauthorized() {
        return "error/unauthorized";
    }

    /**
     * 主页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/home", "/"})
    @ApiOperation(value = "主页面", httpMethod = "GET", notes = "页面接口")
    @RequiresUser
    public String indexPage(Model model) throws Exception {
        //  判断当前账号是否是车间切换到生产计划登记页面
        if (hasRole(RoleConstant.WORKSHOP_ROLE)) {
            // return "redirect:jh_plan_bill_registe";
            return "redirect:jh_plan_bill_registe_sub";
        }
        String name = Optional.ofNullable(configVarcharService
                        .selectByPrimaryKey(ConfigVarEnum.CLIENT_TITLE.getField()))
                .map(x -> x.getConfigValue()).orElse("");
        String userName = Optional.ofNullable(sysUserService.selectByPrimaryKey(getLoginUserId()))
                .map(x -> x.getUserName()).orElse("");
        model.addAttribute(ConfigVarEnum.CLIENT_TITLE.name(), name);
        model.addAttribute(WELCOME, WebUtil.getWelcomeMessage(userName));
        model.addAttribute(REPORT_SERVER_URL, reportURL);
        model.addAttribute(VERSION, version);
        return "index";
    }

    /**
     * 基础资料-用户
     *
     * @return
     */
    @RequestMapping("/jc_user")
    @RequiresPermissions(MenuConstant.MENU502 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-用户", httpMethod = "GET", notes = "页面接口")
    public String jcUserPage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU502);
        return "pages/sz/jc_user";
    }

    /**
     * 设置-角色组
     *
     * @return
     */
    @RequestMapping("/sz_role_group")
    @RequiresPermissions(MenuConstant.MENU503 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "设置-角色组", httpMethod = "GET", notes = "页面接口")
    public String szRoleGroup(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU503);
        return "pages/sz/sz_role_group";
    }

    /**
     * 参数配置
     *
     * @param model
     * @return
     */
    @RequestMapping("/sz_config")
    @RequiresPermissions(MenuConstant.MENU504 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "设置-配置信息", httpMethod = "GET", notes = "页面接口")
    public String szConfig(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU504);
        return "pages/sz/sz_config";
    }

    /**
     * 基础资料-部门员工
     *
     * @return
     */
    @RequestMapping("/jc_employee")
    @RequiresPermissions(MenuConstant.MENU404 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-部门员工", httpMethod = "GET", notes = "页面接口")
    public String jcEmployeePage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU404);
        return "pages/jc/jc_employee";
    }

    /**
     * 基础资料-往来单位
     *
     * @return
     */
    @RequestMapping("/jc_contactcompany")
    @RequiresPermissions(MenuConstant.MENU402 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-往来单位", httpMethod = "GET", notes = "页面接口")
    public String jcContactCompanyPage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU402);
        return "pages/jc/jc_contactcompany";
    }

    /**
     * 基础资料-仓库
     *
     * @return
     */
    @RequestMapping("/jc_store")
    @RequiresPermissions(MenuConstant.MENU405 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-仓库", httpMethod = "GET", notes = "页面接口")
    public String jcStorePage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU405);
        return "pages/jc/jc_store";
    }

    /**
     * 基础资料-权限表
     *
     * @return
     */
    @RequestMapping("/jc_permission")
    @RequiresPermissions(MenuConstant.MENU501 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-权限表", httpMethod = "GET", notes = "页面接口")
    public String jcPermissionPage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU501);
        return "pages/sz/jc_permission";
    }

    /**
     * 基础资料-供应商
     *
     * @return
     */
    @RequestMapping("/jc_supplier")
    @RequiresPermissions(MenuConstant.MENU403 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-供应商", httpMethod = "GET", notes = "页面接口")
    public String jcSupplierPage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU403);
        return "pages/jc/jc_supplier";
    }

    /**
     * 基础资料-单据类型
     *
     * @param model
     * @return
     */
//    @RequestMapping("/jc_bill_type")
//    @RequiresPermissions(MenuConstant.MENU406 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
//    @ApiOperation(value = "基础资料-单据类型", httpMethod = "GET", notes = "页面接口")
//    public String jcBillTypePage(Model model) {
//        model.addAttribute(MENUID, MenuConstant.MENU406);
//        return "pages/jc/jc_bill_type";
//    }

    /**
     * 基础资料-币种
     *
     * @param model
     * @return
     */
    @RequestMapping("/jc_currency_type")
    @RequiresPermissions(MenuConstant.MENU408 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-币种", httpMethod = "GET", notes = "页面接口")
    public String jcCurrencyTypePage(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU408);
        return "pages/jc/jc_currency_type";
    }

    /**
     * 基础资料-剪毛厂
     *
     * @param model
     * @return
     */
    @RequestMapping("/jc_shearing_factory")
    @RequiresPermissions(MenuConstant.MENU409 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-剪毛厂", httpMethod = "GET", notes = "页面接口")
    public String jcShearingFactory(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU409);
        return "pages/jc/jc_shearing_factory";
    }

    /**
     * 基础资料-染厂
     *
     * @param model
     * @return
     */
    @RequestMapping("/jc_dyeing_factory")
    @RequiresPermissions(MenuConstant.MENU410 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-染厂", httpMethod = "GET", notes = "页面接口")
    public String jcDyeingFactory(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU410);
        return "pages/jc/jc_dyeing_factory";
    }

    /**
     * 基础资料-深加工
     *
     * @param model
     * @return
     */
    @RequestMapping("/jc_deep_processing")
    @RequiresPermissions(MenuConstant.MENU411 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "基础资料-深加工", httpMethod = "GET", notes = "页面接口")
    public String jcDeepProcessing(Model model) {
        model.addAttribute(MENUID, MenuConstant.MENU411);
        return "pages/jc/jc_deep_processing";
    }

    /**
     * 订单序时表
     *
     * @param model
     * @return
     */
    @RequestMapping("/jh_order_main_Page")
    @RequiresPermissions(MenuConstant.MENU101 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "计划管理-订单序时表", httpMethod = "GET", notes = "页面接口")
    public String jhOrderMainPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU101);
        return "pages/jh/jh_order_main_Page";
    }

    /**
     * 白坯序时表
     *
     * @param model
     * @return
     */
    @RequestMapping("/yw_grey_main_Page")
    @RequiresPermissions(MenuConstant.MENU201 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "业务流程-白坯序时表", httpMethod = "GET", notes = "页面接口")
    public String ywGreyMainPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU201);
        return "pages/yw/yw_grey_main_Page";
    }

    /**
     * 白坯库存
     *
     * @param model
     * @return
     */
    @RequestMapping("/yw_grey_inventory")
    @RequiresPermissions(MenuConstant.MENU202 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "业务流程-白坯库存", httpMethod = "GET", notes = "页面接口")
    public String ywGreyInventory(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU202);
        return "pages/yw/yw_grey_inventory";
    }

    /**
     * 色布序时表
     *
     * @param model
     * @return
     */
    @RequestMapping("/yw_half_main_Page")
    @RequiresPermissions(MenuConstant.MENU203 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "业务流程-色布序时表", httpMethod = "GET", notes = "页面接口")
    public String ywHalfMainPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU203);
        return "pages/yw/yw_half_main_Page";
    }

    /**
     * 成品序时表
     *
     * @param model
     * @return
     */
    @RequestMapping("/yw_product_main_Page")
    @RequiresPermissions(MenuConstant.MENU204 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "业务流程-成品序时表", httpMethod = "GET", notes = "页面接口")
    public String ywProductMainPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU204);
        return "pages/yw/yw_product_main_Page";
    }

    /**
     * 成品库存
     *
     * @param model
     * @return
     */
    @RequestMapping("/yw_product_inventory")
    @RequiresPermissions(MenuConstant.MENU205 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "业务流程-成品序时表", httpMethod = "GET", notes = "页面接口")
    public String ywProductInventory(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU205);
        return "pages/yw/yw_product_inventory";
    }

    /**
     * 白坯应付款
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_payment_supplier_Page")
    @RequiresPermissions(MenuConstant.MENU301 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-白坯应付款", httpMethod = "GET", notes = "页面接口")
    public String cwPaymentSupplierPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU301);
        return "pages/cw/cw_payment_supplier_Page";
    }

    /**
     * 染厂应付款
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_payment_dyeing_Page")
    @RequiresPermissions(MenuConstant.MENU302 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-染厂应付款", httpMethod = "GET", notes = "页面接口")
    public String cwPaymentDyeingPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU302);
        return "pages/cw/cw_payment_dyeing_Page";
    }

    /**
     * 深加工应付款
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_payment_deep_Page")
    @RequiresPermissions(MenuConstant.MENU303 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-深加工应付款", httpMethod = "GET", notes = "页面接口")
    public String cwPaymentDeepPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU303);
        return "pages/cw/cw_payment_deep_Page";
    }

    /**
     * 成品应收款
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_payment_client_Page")
    @RequiresPermissions(MenuConstant.MENU304 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-成品应收款", httpMethod = "GET", notes = "页面接口")
    public String cwPaymentClientPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU304);
        return "pages/cw/cw_payment_client_Page";
    }

    /**
     * 订单综合查询表
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_order_sum_Page")
    @RequiresPermissions(MenuConstant.MENU305 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-订单综合查询表", httpMethod = "GET", notes = "页面接口")
    public String cwOrderSumPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU305);
        return "pages/cw/cw_order_sum_Page";
    }


    /**
     * 客户订单明细表
     *
     * @param model
     * @return
     */
    @RequestMapping("/cw_order_detail_Page")
    @RequiresPermissions(MenuConstant.MENU306 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-客户订单明细表", httpMethod = "GET", notes = "页面接口")
    public String cwOrderDetailPage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU306);
        return "pages/cw/cw_order_detail_Page";
    }

    @RequestMapping("/cw_order_detail_date_Page")
    @RequiresPermissions(MenuConstant.MENU307 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-订单明细叙时表", httpMethod = "GET", notes = "页面接口")
    public String cwOrderDetailDatePage(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU307);
        return "pages/cw/cw_order_detail_date_Page";
    }

    @RequestMapping("/cw_allysk_yfk_sum")
    @RequiresPermissions(MenuConstant.MENU310 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-应收应付汇总", httpMethod = "GET", notes = "页面接口")
    public String cwAllyskYfkSum(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU310);
        return "pages/cw/cw_allysk_yfk_sum";
    }

    @RequestMapping("/cw_sk_fk_detail")
    @RequiresPermissions(MenuConstant.MENU311 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @ApiOperation(value = "财务管理-已收款已付款明细", httpMethod = "GET", notes = "页面接口")
    public String cwSkFkDetail(Model model, Long id) {
        if (id != null)
            model.addAttribute("id", id);
        model.addAttribute(MENUID, MenuConstant.MENU311);
        return "pages/cw/cw_sk_fk_detail";
    }
}
